/**
 * Project: saasoa
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.web.servlet.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thx.common.util.AppContext;
import com.topdt.jbpm.identity.Department;
import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.Role;
import com.topdt.jbpm.utils.TreeUtils;
import com.topdt.jbpm.web.servlet.JbpmServlet;

/**
 * 建立日期 : 2013-7-6 上午11:23:54<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class IdentityAction extends JbpmBaseAction {

	public IdentityAction(JbpmServlet servlet) {
		super(servlet);
	}

	public void asynLoadOrgs(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("<进入>:asynLoadOrgs方法");
		String id = request.getParameter("id");

		id = id == null ? "0" : id;

		// this.idService

		List<Department> deps = null;

		List<TreeNode> nodes = new ArrayList<TreeNode>();

		// for (Department dep : deps) {
		for (int i = 0; i < 4; i++) {
			TreeNode node = new TreeNode();
			// node.setId(dep.getId());
			node.setId(id + "-" + String.valueOf(i));
			// node.setText(dep.getName());
			node.setText("node" + i);
			node.setState("closed");// TODO open
			nodes.add(node);
		}
		try {
			this.servlet.responseJson(response, nodes);
		} catch (IOException e) {
			logger.error("<错误>:" + e.getMessage());
		}
	}

	class TreeNode {
		private String id;
		private String text;
		private String state;
		private String iconCls;
		private boolean checked;
		private List<TreeNode> children;
		private List<Attribute> attributes;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public String getState() {
			return state;
		}

		public void setState(String state) {
			this.state = state;
		}

		public String getIconCls() {
			return iconCls;
		}

		public void setIconCls(String iconCls) {
			this.iconCls = iconCls;
		}

		public boolean isChecked() {
			return checked;
		}

		public void setChecked(boolean checked) {
			this.checked = checked;
		}

		public List<TreeNode> getChildren() {
			return children;
		}

		public void setChildren(List<TreeNode> children) {
			this.children = children;
		}

		public List<Attribute> getAttributes() {
			return attributes;
		}

		public void setAttributes(List<Attribute> attributes) {
			this.attributes = attributes;
		}

	}

	class Attribute {
		private String url;

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}
	}

	public void getSelectUserPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("<进入>:getSelectUserPage方法");
		IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
		List<Department> deps = idService.getAllDepsAndUsers(AppContext.getCurrentComId());
		TreeUtils.getJsonFile(idService.getOptionsByDepartment(deps), "dept_users", null);// TODO
																							// 调整路径

		// List<Role> roles =idService.getAllRoles(sessionInfo.getCompanyId());
		List<Role> roles = idService.getTreeRoles(AppContext.getCurrentComId());
		TreeUtils.getJsonFile(idService.getOptionsByRoles(roles), "role_users",null);//TODO 调整路径

		RequestDispatcher rd = null;

		rd = request.getSession().getServletContext().getRequestDispatcher("/pages/workflow/freeFlow/selectUser.jsp"); // 定向的页面

		rd.forward(request, response);
	}

	public void getUsersM(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("<进入>:getUsersM方法");
		IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
		List<Department> deps = idService.getAllDepsAndUsers(AppContext.getCurrentComId());
		String json = TreeUtils.getUsersTreeM(idService.getOptionsByDepartment(deps));

		/*
		 * List<Role> roles =idService.getAllRoles(sessionInfo.getCompanyId());
		 * TreeUtils.getJsonFile(idService.getOptionsByRoles(roles),
		 * "role_users");
		 */
		this.servlet.responseJson(response, json);
	}

}
